// 处理业务逻辑的中间件,读取某个json文件的数据
const path = require("path");
const fileUtils = require("../utils/file_utils");

module.exports = async(ctx,next) => {
    // 根据url
    const url = ctx.request.url; // /api/seller 
    let filePath = url.replace("/api",""); // /seller
    filePath = "../data" + filePath + ".json"; // ../data/seller.json
    filePath = path.join(__dirname,filePath); // 绝对路径

    try{
        const res = await fileUtils.getFileJsonData(filePath);
        ctx.response.body = res;
    }catch(e){
        const errorMsg = {
            message: "读取文件内容失败,文件资源不存在",
            status:404
        }
        ctx.response.body = JSON.stringify(errorMsg);
    }

    console.log(filePath);
    await next();
}
